<?php
require_once "../../_inc/funct.xml2array.php";
function getCerBilXml($codCCIAA,$dataRif,&$sXml) {
	global $objDB, $objSess;
	$codCCIAA = strtoupper($codCCIAA);
	$filename = RGL_FS_BIL_DIR.'/'.substr($codCCIAA,0,2).'-'.substr($codCCIAA,2)
		.'-710-'.YMD2DMY($dataRif,'-').'.xml';
	if (!file_exists($filename)) {
		return 'Bilancio '.$filename.' non trovato';
	}
	$sXml = file_get_contents($filename);
	return '';
}

function cerBilXml2aInfo($isSetDati,&$sXml,&$aInfo) {
	global $objDB, $objSess;
	// carica raccordo voce CEE - voce eval
	$sSql = "SELECT d.cod_fonte, d.cod_dato FROM rgl_def_dato d " .
			" WHERE d.id_set_dati = $isSetDati and d.tipo_tab_dati = 1 ";
	$objDB->query($sSql, 'lisvoc');
	$aRacc = array();
	while($row = $objDB->fetch('lisvoc')) {
		$aRacc[$row['cod_fonte']] = $row['cod_dato'];
	}
	//dbg_($aRacc);
	$aW = xml2array($sXml,array('withAttr'=>true));
	//dbg_(x2vPrint($aW));
	if(rglVx($aW,'esito','cod') != '200') {
		return rglVx($aW,'esito','descr')." (".rglVx($aW,'esito','cod').")";
	}
	$aZ = rglVx($aW,'aXml','s2xResponse',0,'s2xData',0,'BalanceSheetIdentification',0);
	//dbg_($aZ);
	$aInfo['anag']['rag_soc'] = rglVx($aZ,'CompanyName',0);
	$aInfo['anag']['cod_fisc'] = rglVx($aZ,'TaxCode',0);
	$aInfo['anag']['comune'] = rglVx($aZ,'Residence',0,'Municipality',0);
	$aInfo['anag']['prov'] = rglVx($aZ,'Residence',0,'Province',0);
	$aInfo['anag']['cod_att'] = rglVx($aZ,'AtecoCode02',0);
	$aInfo['anag']['attivita'] = rglVx($aZ,'AtecoCode02','0A','Description');
	
	$aAccSt = rglVx($aW,'aXml','s2xResponse',0,'s2xData',0,'BalanceSheetIdentification',0
		,'SectionAccountStatement',0,'AccountStatement');
	$aDati = array();
	foreach($aAccSt as $akX => $aX) {
		$val = rglVx($aX,'Amount',0)/100;
		$cod = rglVx($aX,'ItemCode',0);
		if ($val != 0) $aDati[$aRacc[$cod]] = $val;		
		//dbg_("$cod - $descr - $val");
	}
	$aInfo['dati'] = $aDati;
	return '';
}

